<template>
  <div>
    <Row>
      <Card>
        <p slot="title">
          <Icon type="navicon-round"></Icon>
          摄像头管理
        </p>
        <VmBtable type="edit" ref="VmBtable"
                  :columns="dataColumns"
                  :data="dataTable"
                  :searchparam="searchParam"
                  :exportBtn="false"
                  :importBtn="false"
                  :batchDelBtn="false"
                  :downTemplate="false"
                  v-on:searchFn="searchFn"
                  v-on:page-change="pageChangeFn"
                  v-on:addFn="addFn"
                  v-on:editFn="editFn"
                  v-on:deleteFn="deleteFn"
                  placeholder="摄像头名称关键字"
                  searchText="摄像头名称："
                  cleanText="刷新"
                  v-on:restFn="restFn">
          <div slot="search" class="db_inline">

          </div>
        </VmBtable>
      </Card>
    </Row>
    <Modal v-model="modal.visible" :title="modal.title" v-on:on-ok="subForm" :mask-closable="false" width="680"
           v-on:on-cancel="cancelSub">
      <Form ref="Form" :model="modal.form" :label-width="80" label-position="right" :rules="modal.rules">
        <FormItem label="名称" prop="name">
          <Input v-model="modal.form.name" placeholder="请输入名称" style="width:83%"/>
        </FormItem>
        <FormItem label="区域属性" prop="common" class="db_inline">
          <RadioGroup v-model="modal.form.common">
            <Radio :label=1>公共区域</Radio>
            <Radio :label=0>私有区域</Radio>
          </RadioGroup>
        </FormItem>
        <!--    <FormItem label="是否开放" prop="open" class="db_inline">
              <RadioGroup v-model="modal.form.open">
                <Radio :label=1>开放</Radio>
                <Radio :label=0>不开放</Radio>
              </RadioGroup>
            </FormItem>-->
        <FormItem label="班级" prop="clazzId" v-if="modal.form.common===0">
          <Select v-model="modal.form.clazzId" style="width:83%">
            <Option v-for="item in clazzList" :value="item.id" :key="item.id">{{ item.name }}</Option>
          </Select>
        </FormItem>
        <FormItem label="url地址" prop="url">
          <Input v-model="modal.form.url" type="textarea" placeholder="请输入url地址" style="width:83%"/>
        </FormItem>
        <FormItem label="备注" prop="remark">
          <Input v-model="modal.form.remark" type="textarea" placeholder="备注" style="width:83%"/>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" style="color: #3399ff;cursor: pointer" @click="cancelSub">取消</Button>
        <Button type="primary" @click="subForm" :loading="modal.loading">确定</Button>
      </div>
    </Modal>
    <Modal v-model="video.visible" :title="video.title" width="350">
      <div style="height: 200px;text-align: center">
        <video-player v-if="video.visible" class="vjs-custom-skin"
                      ref="videoPlayer"
                      :options="video.options"
                      :playsinline="true">
        </video-player>
      </div>
    </Modal>
  </div>
</template>

<script>
  import API from "@/api/api";
  import VmBtable from "@/components/vm/vm-btable";
  import 'videojs-flash'
  import 'videojs-hotkeys'

  export default {
    components: {
      VmBtable
    },
    data() {
      return {
        searchParam: {
          current: 1,
          size: 20,
          total: 0,
          name: '',
        },
        dataColumns: [
          {
            title: '序号',
            type: 'index',
            align: 'center',
            width: 100
          },
          {
            title: '名称',
            align: 'center',
            key: 'name',
          },
          {
            title: '区域属性',
            align: 'center',
            key: 'common',
            render: (h, param) => {
              let common = param.row.common;
              return h('div', {}, common === 1 ? '公共区域' : '私有区域')
            }
          },
          {
            title: 'url地址',
            align: 'center',
            key: 'url',
            minWidth: 280,
            render: (h, param) => {
              return h('div', {
                style: {
                  color: '#3399ff',
                  cursor: 'pointer'
                },
                on: {
                  click: () => {
                    this.playFn(param.row)
                  }
                }
              }, param.row.url);
            }
          },
          {
            title: '备注',
            align: 'center',
            key: 'remark',
            minWidth: 100,
          },
        ],
        operate: [],
        dataTable: [],
        clazzList: [],
        modal: {
          visible: false,
          title: '',
          isEdit: false,
          loading: false,
          form: {
            name: '',
            open: 0,
            common: 1,
          },
          rules: {
            'name': [{
              required: true,
              trigger: 'blur',
              message: '必填项'
            }],
            'url': [{
              required: true,
              trigger: 'blur',
              message: '必填项'
            }]
          }
        },
        video: {
          title: '',
          visible: false,
          height: '360',
          options: {
            muted: false,
            autoplay: true,
            techOrder: ['flash'],
            sources: []
          }
        }
      };
    },
    methods: {
      playFn(data) {
        let url = data.url;
        if (url.indexOf('.flv') === -1) {
          url = url.substring(0, url.lastIndexOf(".")) + '.flv';
        }
        let source = {type: 'video/x-flv', src: url};
        this.video.options.sources.push(source);
        this.video.visible = true;
      },
      addFn() {
        Object.assign(this.modal, {
          visible: true,
          isEdit: false,
          title: '添加',
          form: {common: 1}
        })
      },
      editFn(data) {
        Object.assign(this.modal, {
          visible: true,
          isEdit: true,
          title: '编辑',
          form: data
        })
      },
      deleteFn(data) {
        let id = data[0].id;
        API.deleteCamera({id: id}).then(resp => {
          this.queryFn();
        })
      },
      restFn() {
        this.searchParam.current = 1;
        this.searchParam.size = 20;
        this.searchParam.name = '';
        this.queryFn();
      },
      queryFn() {
        API.fetchCamera(this.searchParam).then(
          res => {
            let data = res.data;
            this.dataTable = data.records;
            this.searchParam.total = data.total;
          },
          error => {
            this.$Message.info(error.msg);
          }
        );
      },
      complete() {
        Object.assign(this.modal, {
          visible: false,
          title: '',
          loading: false,
        });
        this.$refs.Form.resetFields();
      },
      subForm() {
        this.$refs.Form.validate(valid => {
          if (valid) {
            let data = Object.assign(this.modal.form);
            if (data.id) {
              API.updateCamera(data).then(resp => {
                this.queryFn();
                this.complete();
              })
            } else {
              API.createCamera(data).then(resp => {
                this.queryFn();
                this.complete();
              })
            }
          }
        })
      },
      cancelSub() {
        this.complete();
      },
      pageChangeFn(current) {
        this.searchParam.current = current;
        this.queryFn();
      },
      searchFn(value) {
        this.searchParam.name = value;
        this.queryFn();
      },
      fetchKindergartenClazz() {
        API.fetchKindergartenClazz().then(resp => {
          this.clazzList = resp.data;
        })
      }
    },
    created() {
    },
    mounted() {
      this.queryFn();
      this.fetchKindergartenClazz();
    }
  }
</script>

<style scoped>
</style>
